#include "Storage.h"

Storage::~Storage()
{
	delete myfile;
}

void Storage::init_filemanager(string filename)
{
	myfile=new fileManager(filename);
}
string Storage::schedule_encoder(Schedule* schedule)
{
	string text_line;
	text_line+=schedule->get_day()+SPILT_PARAMETER;
	text_line+=schedule->get_start_time()+SPILT_PARAMETER;
	text_line+=schedule->get_end_time()+SPILT_PARAMETER;
	text_line+=schedule->get_name()+SPILT_PARAMETER;
	text_line+=schedule->get_place()+SPILT_PARAMETER;
	text_line+=Schedule::priority_converter(schedule->get_priority())+SPILT_PARAMETER;
	return text_line;
}

Schedule* Storage::schedule_decoder(string textSchedule)
{
	//we haven't touched the priority yet
	size_t found;
	string list[NUM_OF_PARAMETER];
	for(int i=0;i<NUM_OF_PARAMETER;i++)
	{
		if(textSchedule!="")
		{
			found=textSchedule.find(SPILT_PARAMETER);
			if(int(found)<textSchedule.size())
			{
				list[i]=textSchedule.substr(0,int(found));
				textSchedule.erase(0,int(found)+SPILT_PARAMETER.size());
			}
		}
	}
	Schedule::LEVEL priority = Schedule::priority_converter(list[5]);
	//Parameter order: day - start time - end time - name - place - priority
	Schedule* result= new Schedule(list[0],list[1],list[2],list[3],list[4],priority);
	return result;
}
void Storage::add_schedule(Schedule* schedule)
{
	string codedSchedule= this->schedule_encoder(schedule);
	myfile->add_new_line(codedSchedule);
}

void Storage::remove_schedule(int index)
{
	myfile->delte(index+1);
}

Schedule* Storage::schedule_at_index(int index)
{
	//notice: for the fileManager, 1st element is at index=1
	//for the vector, 1st element is at index=0
	string text_schedule=myfile->get_line(index+1);
	return this->schedule_decoder(text_schedule);
}

int Storage::find_schedule_index(Schedule* schedule)
{
	vector<string> textlist=myfile->get_all_line();
	string text_schedule=this->schedule_encoder(schedule);
	for(unsigned i=0;i<textlist.size();i++)
	{
		if(textlist[i]==text_schedule)
		{
			return i;
			//CONVENTION: index for list start from 0
			//only index of fileManager start from 1
		}
	}
	return -1;
}

vector<Schedule*> Storage::search_schedule(string keyword,vector<int> &index,Schedule::DATATYPE type)
{
	keyword=to_lower_case(keyword);
	vector<Schedule*> found_list;
	vector<string> text_list=myfile->get_all_line();
	for(int i=0;i<text_list.size();i++)
	{
		Schedule* temp=schedule_decoder(text_list[i]);
		string checkingInfo=to_lower_case(temp->get(type));

		size_t found;
		found=checkingInfo.find(keyword);
		if(found!=string::npos)
		{
			found_list.push_back(temp);
			index.push_back(i);
		}
	}
	return found_list;
}

vector<Schedule*> Storage::get_all_schedule(vector<int> &index)
{
	vector<Schedule*> found_list;
	vector<string> text_list=myfile->get_all_line();
	for(int i=0;i<text_list.size();i++)
	{
		Schedule* temp=schedule_decoder(text_list[i]);
		found_list.push_back(temp);
		index.push_back(i);
	}
	return found_list;
}